package Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @author PZK
 *
 */
public class Listener extends Thread {
	
	private ClientHandler clientHandler;
	private BufferedReader in;
	private Client client;
	
	public Listener(ClientHandler clientHandler, Socket socket, Client client){
		
		this.clientHandler = clientHandler;
		this.client = client;
		
		try{
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		}catch(IOException e){
			e.printStackTrace();
		}
		
		try {
			client.setSystemCode(in.readLine());
			client.setRegexCode(in.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		start();
		
	}
	
	@Override
	public void run(){
		
		try{
			while(!isInterrupted()){
				String message = in.readLine();
				System.out.println("RECEIVED: "+message);
				clientHandler.processMessage(client, message);
			}	
		}catch(IOException e){
			//connection interrupted
		}
		
		clientHandler.removeClient(client);
		
	}

}
